package com.xiaoshuidi.cloud.framework.pay.core.client.impl.yinlian.app;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xiaoshuidi.cloud.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.xiaoshuidi.cloud.framework.pay.core.client.PayCommonResult;
import com.xiaoshuidi.cloud.framework.pay.core.client.dto.*;
import com.xiaoshuidi.cloud.framework.pay.core.client.impl.tongshangyun.dto.EnterpriseAccountEstablishReqDTO;
import com.xiaoshuidi.cloud.framework.pay.core.client.impl.tongshangyun.dto.EnterpriseAccountEstablishRespDTO;
import com.xiaoshuidi.cloud.framework.pay.core.client.impl.yinlian.YinlianClientConfig;
import com.xiaoshuidi.cloud.framework.pay.core.client.impl.yinlian.YinlianCodeMapping;
import com.xiaoshuidi.cloud.framework.pay.core.enums.PayChannelEnum;
import com.xiaoshuidi.cloud.framework.pay.core.enums.PayDisplayModeEnum;
import lombok.extern.slf4j.Slf4j;

/**
 * 银联支付宝App支付
 */
@Slf4j
public class YinlianAlipayAppPayClient extends AbstractYinlianAppPayClient {

    public YinlianAlipayAppPayClient(Long channelId, YinlianClientConfig config) {
        super(channelId, PayChannelEnum.YINLIAN_WX_MINIAPP.getCode(), config, new YinlianCodeMapping());
    }

    @Override
    public PayRefundNotifyDTO parseRefundNotify(PayNotifyDataDTO notifyData) {
        return null;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected PayCommonResult<PayOrderUnifiedRespDTO<ObjectNode>> doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) throws Throwable {
        ObjectNode json = baseParams(config.getMerchantCode(), config.getTerminalCode());
        json.put("merOrderId", nextOrderNo()); // 商户订单号
        json.put("totalAmount", reqDTO.getAmount());
        json.put("notifyUrl", reqDTO.getNotifyUrl());
        json.put("instMid", "APPDEFAULT");
        return call("/v1/netpay/trade/precreate", json, (code, resp) -> new PayOrderUnifiedRespDTO<ObjectNode>()
                .setMerchantOrderId(json.get("merOrderId").asText())
                .setDisplayContent(code == GlobalErrorCodeConstants.SUCCESS ? resp.get("appPayRequest").toString() : null)
                .setDisplayMode(PayDisplayModeEnum.APP.getMode())
                .setRawRequest(json.toString())
                .setRawResponse(resp));
    }

}
